<template>
  <lay-layer v-model="visible11" :title="actionsMap[action]" :area="['500px']">
    <div style="padding: 20px">
      <lay-form :model="form" ref="layFormRef11" required label-width="110px">
        <lay-form-item label="待办标题" prop="title">
          <lay-input v-model="form.title" />
        </lay-form-item>
        <lay-form-item label="待办内容" prop="content">
          <lay-input v-model="form.content" />
        </lay-form-item>
        <lay-form-item label="开始日期" prop="startDate">
          <lay-date-picker v-model="form.startDate" placeholder="选择开始日期" allow-clear></lay-date-picker>
        </lay-form-item>
        <lay-form-item label="开始时间" prop="startTime">
          <lay-date-picker v-model="form.startTime" placeholder="选择开始时间" allow-clear type="time"></lay-date-picker>
        </lay-form-item>
        <lay-form-item label="结束日期" prop="endDate">
          <lay-date-picker v-model="form.endDate" placeholder="选择结束日期" allow-clear></lay-date-picker>
        </lay-form-item>
        <lay-form-item label="结束时间" prop="endTime">
          <lay-date-picker v-model="form.endTime" placeholder="选择结束时间" allow-clear type="time"></lay-date-picker>
        </lay-form-item>
      </lay-form>
      <div style="width: 100%; text-align: center">
        <lay-button size="sm" type="primary" @click="toSubmit">保存</lay-button>
        <lay-button size="sm" @click="toCancel">取消</lay-button>
      </div>
    </div>
  </lay-layer>
</template>

<script setup lang="ts">
import { actionsMap } from '@/dicts/actions';
import { computed, onMounted, ref } from 'vue';
import { cloneDeep } from 'lodash-es'
import { layer } from '@layui/layui-vue';
import { dayBooksApi } from '@/api/module/dayBooks';
import { cardTypesMap, tradingPlatformsMap } from '@/dicts/dayBooks';
import { todosApi } from '@/api/module/todos';

const action = ref('add'); // add | update
const visible11 = ref(false)
const form: any = ref({})

const emits = defineEmits(['success'])

defineExpose({
  open
})

onMounted(() => {

})

function open(actionKey: string, row: any = {}) {
  action.value = actionKey;
  form.value = cloneDeep(row || {});
  visible11.value = true;
}

function toSubmit() {
  console.log(form.value, 'form');
  if (form.value.id) {
    todosApi.update(form.value).then(() => {
      layer.msg('保存成功！', { icon: 1, time: 1000 });
      visible11.value = false;
      emits('success')
    })


  } else {
    todosApi.add(form.value).then(() => {
      layer.msg('保存成功！', { icon: 1, time: 1000 });
      visible11.value = false;
      emits('success')
    })
  }
}

function toCancel() {
  visible11.value = false
}
</script>